<template>
  <div class="cc-business">
    <div v-for="(val, key) in tableData" :key="key">
      <h3>{{ key === "basic" ? "基础字段" : "自定义字段" }}</h3>
      <el-table :data="val" border style="width: 100%">
        <el-table-column
          prop="name"
          label="字段名称"
          width="120"
          show-overflow-tooltip
        />
        <el-table-column
          prop="value"
          label="字段值"
          width="200"
          show-overflow-tooltip
        >
          <template slot-scope="{ row }">
            <span v-if="row.key === 'comment'" v-html="row.value"></span>
            <span v-else>{{ row.value }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Prop } from 'vue-property-decorator'
import { commonAction } from '@/utils/common'
@Component({
  name: 'OldCusBusiness'
})
export default class OldCusBusiness extends Vue {
  private tableData: any = {
    basic: [],
    custom: []
  }

  private basicFieldName (field: string) {
    switch (field) {
      case 'number':
        return '工单编号'
      case 'priority':
        return '工单优先级'
      case 'customer':
        return '关联客户'
      case 'flow':
        return '工单类型'
      case 'status':
        return '工单状态'
      case 'master':
        return '当前处理人'
      case 'createTime':
        return '工单创建时间'
      case 'createUser':
        return '工单创建人'
      case 'processEndTime':
        return '工单结束时间'
      case 'createMode':
        return '工单来源'
      case 'comment':
        return '留言板'
    }
  }

  public initData (val: any) {
    const params = {
      url: '/server-crm/action/customer/dynamics/business/query',
      method: 'POST',
      data: {
        ccAccountId: val.ccAccountId,
        number: val.content.orderNo
      }
    }
    commonAction(params).then(res => {
      if (res.success && res.data) {
        this.tableData.basic = [] // 清空数据
        this.tableData.custom = [] // 清空数据
        for (const key in res.data) {
          if (key === 'businessFields') { // 自定义字段
            this.tableData.custom = res.data[key] || []
          } else { // 基础字段
            const fieldObj: any = {}
            fieldObj.key = key
            fieldObj.name = this.basicFieldName(key)
            if (key === 'comment' && res.data[key]) { // 留言板
              fieldObj.value = res.data[key].length ? res.data[key].map(item => `<p>${item.agent + ' ' + item.comment}</p>`).join('') : '--'
              console.log(fieldObj.value, 77)
            } else if (key === 'createMode') {
              fieldObj.value = '接口创建'
            } else if (key === 'status') {
              fieldObj.value = res.data[key] === 'complete' ? '已完成' : '已取消'
            } else {
              fieldObj.value = res.data[key]
            }
            this.tableData.basic.push(fieldObj)
          }
        }
      }
    })
  }
}
</script>

<style lang="stylus" scoped>
.cc-business
  display flex
  padding 20px
  h3
    margin-bottom 10px
    font-weight bold
  >>> .el-table
    .cell
      font-weight normal
    .el-table__empty-block
      min-height 48px
    .el-table__empty-text
      line-height 48px
</style>
